﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 添加养殖企业信息命令处理
    /// </summary>
    public class AddFarmingCorporateCommandHandlter : IRequestHandler<AddFarmingCorporateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 养殖企业信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingCorporateModel> farmingCorporateRep;
        /// <summary>
        ///映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingCorporateRep">养殖企业信息仓储</param>
        /// <param name="mapper">映射</param>
        public AddFarmingCorporateCommandHandlter(IBaseRepository<FarmingCorporateModel> farmingCorporateRep,IMapper mapper)
        {
            this.farmingCorporateRep = farmingCorporateRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(AddFarmingCorporateCommand request, CancellationToken cancellationToken)
        {


            //添加养殖企业信息
            var corporate=mapper.Map<FarmingCorporateModel>(request);
            corporate.Creatime = DateTime.Now;
            await farmingCorporateRep.AddAsync(corporate);

            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加企业信息成功"

            };
        }
    }
}
